package com.ruoyi.app.business.dict;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.app.converter.DictConverter;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.service.dict.entity.DictDataEntity;
import com.ruoyi.service.dict.enums.DictTypeEnum;
import com.ruoyi.service.dict.model.DictModel;
import com.ruoyi.service.dict.service.DictDataService;
import com.ruoyi.service.dict.service.DictTypeService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 作用：字典
 * </p>
 *
 * @author Zane
 * @since 2025-10-04 8:52
 */
@Slf4j
@Component
public class DictBusiness {

  @Resource
  private DictDataService dictDataService;
  @Autowired
  private DictTypeService dictTypeService;

  public CommonPageInfo<DictModel> getPageByType(String dictType) {
    CommonPageInfo<DictDataEntity> pageInfo = PageUtils.startPage();
    pageInfo.initData(Optional.ofNullable(dictDataService.listByDictType(dictType))
        .orElseGet(ArrayList::new));
    return pageInfo.convert(DictConverter.INSTANCE::toDictModel);
  }

  public List<DictDataEntity> getListByType(String dictType) {
    return dictDataService.listByDictType(dictType);
  }

  public Map<String, DictDataEntity> getMapByType(String dictType) {
    return Optional.ofNullable(this.getListByType(dictType)).orElseGet(ArrayList::new)
        .stream()
        .collect(Collectors.toMap(DictDataEntity::getDictCode, v -> v, (v1, v2) -> v1));
  }

  public void checkDictDesc(DictTypeEnum dictTypeEnum, List<String> dictDescList) {
    checkDictDescAndGetMap(dictTypeEnum.getCode(), dictDescList);
  }

  public void checkDictDesc(String dictType, List<String> dictDescList) {
    checkDictDescAndGetMap(dictType, dictDescList);
  }

  public Map<String, String> checkDictDescAndGetMap(DictTypeEnum dictTypeEnum,
      List<String> dictDescList) {
    return checkDictDescAndGetMap(dictTypeEnum.getCode(), dictDescList);
  }

  public Map<String, String> checkDictDescAndGetMap(String dictType, List<String> dictDescList) {
    // 需要校验的字典名
    if (CollUtil.isEmpty(dictDescList)) {
      throw new ServiceException("请选择字典值");
    }
    //
    final List<DictDataEntity> dictDataEntities = dictDataService.listByDictType(dictType);
    final Map<String, String> dicMap = dictDataEntities.stream()
        .collect(Collectors.toMap(DictDataEntity::getDictDesc, DictDataEntity::getDictCode));
    final String dictTypeName = dictTypeService.getByTypeCode(dictType).getDictName();
    dictDescList.forEach(dictDesc -> {
      if (!dicMap.containsKey(dictDesc)) {
        throw new ServiceException(StrUtil.format("{}【{}】不存在", dictTypeName, dictDesc));
      }
    });
    return dicMap;
  }

  public List<DictModel> getListModelByType(String dictType) {
    final List<DictDataEntity> dictDataEntities = Optional.ofNullable(
            dictDataService.listByDictType(dictType))
        .orElseGet(ArrayList::new);
    return dictDataEntities.stream().map(DictConverter.INSTANCE::toDictModel)
        .collect(Collectors.toList());
  }
}
